/*
* Copyright 2012	Team#6, F2-101, Neusoft
*
* This file defines common formats for Linpop, a LAN chatting program
* Linpop follows the IP Messenger communication protocol (Draft-9)
* you can obtain a copy of Linpop at
*
*	http://code.google.com/p/linpop
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Apache 2.0 License.
*/

#ifndef COMMON_FORMATS_H
#define COMMON_FORMATS_H

#include <unistd.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/time.h>

#define BUFLEN 1024
#define NAMELEN 50
#define MSGLEN  1000
#define COMLEN  1500
#define RECFRG  1448
#define HSIZE   10
#define FILENAME 128
#define MSGLIMIT 100
#define HL_HEADERSIZE	4
#define HL_FILESIZE	9
#define HL_FILETYPE	1
#define HL_1416		11
#define CAPACITY     50
#define PORT 9741
#define SETTING_PORT 9740
#define HEARTRBEAT 54319



#define LOGIN_PORT 54321
#define REGISTER_PORT 54320
#define MSG_PORT 54319

#define UL unsigned long
#define UNIT unsigned int
#define ERR(x) {perror("x"); exit(1);}
//const char allHosts[] = "255.255.255.255";

typedef struct HeartBeat
    {
        char name[NAMELEN];
        int now_time;
        int last_time;
    } heatbeat;

typedef struct ClientRequest         //! Request from Clients to Server
{
    unsigned int requestType;
	char senderName[NAMELEN];
	char destName[NAMELEN];
	char senderPasswd[9];
	char sourceIP[16];
	char destIP[16];
	char nickName[NAMELEN];
	int state;
	char gender[NAMELEN];
	unsigned int age;
	char branch[NAMELEN];
	char telephone[12];
	char birthdate[12];
	unsigned int profilepic;
	char msg[MSGLEN];

} clientRequest;

typedef struct IP_NAME
{
    char name[NAMELEN];
    char IP[16];
}ip_Name;

typedef struct FileNode                       //!File Pocket
{
    int    tcpSock;                   //socket for file transmissions in TCP
    unsigned int    fileNo;
    char   fileName[FILENAME];
    char   fileSize[NAMELEN];
    char   mtime[NAMELEN];            //ChangeTimeStamp
    int    fileType;
    char   otherAttrs[2*NAMELEN];
    struct FileNode* next;            //File Links
} fileNode;

typedef struct Command                //!COMMAND for Transmission
{
    unsigned int version;             //IPMSG Protocol Version, =1
    unsigned int  packetNo;
    char        senderName[NAMELEN];
    char        senderHost[NAMELEN];
    unsigned int commandNo;           //Command Word defines in ipmsg.h
    char        additional[MSGLEN];   //UDPmessage Content
    struct sockaddr_in peer;          //Socket Struct
    fileNode  *fileList;              //File Links
    struct Command *next;             //Command Links
} command;

typedef struct GsNode                 //!Dir/multi-files
{
    int tcpSock;
    struct sockaddr_in peer;
    unsigned int packetNo;
    int	 transferring;                //transfer flag
    int    cancelled;
    char  *targetDir;
    fileNode fileList;
    struct GsNode *next;
} gsNode;

typedef struct User                 //! User Storage(Links)
{
    struct sockaddr_in peer;
    char name[NAMELEN];
    char host[NAMELEN];
    char nickname[NAMELEN];
    int inUse;
    int isExit;
    struct User *next;
} user;

typedef struct MsgList              //! Received Msg Storage(Links)
{
    command comHead;
    command *comTail;
} msgList;

typedef int (*Mysnd)(int, const char*, int, int); //I dont know what this for

extern const char allHosts[];       //Boardcasting Address
extern int msgSock;                 //MsgRecvSend Socket
extern int tcpSock;                 //FileRecvSend Socket
extern struct passwd* pwd;
extern struct utsname sysName;
extern char workDir[FILENAME];

extern gsNode sendFHead, getFHead;  //File list for Send/Recv
extern msgList mList;               //Msg List having been obtain

extern pthread_mutex_t sendFMutex;      //Sending File
extern pthread_mutex_t getFMutex;       //Recving File
extern pthread_mutex_t msgMutex;        //Msg Queue
extern pthread_mutex_t usrMutex;        //Usr Queue
extern sem_t waitNonEmpty, waitNonFull; //MsgList semaphore

//extern int msgParser(char *msg, int size, command* com);
//extern int msgCreator(char* msg, command* com, size_t msgLen);
extern fileNode* getFilelist(const char* comFiles);
//extern void initCommand(command *com, unsigned int flag);
extern void deCommand(command *com);
extern void initGsNode(gsNode *gs);
extern void deGsNode(gsNode *gs);

#endif //COMMON_FORMATS_H
